﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace CECB.Member
{
    /// <summary>
    /// Interaction logic for V_MemberNew_Window.xaml
    /// </summary>
    public partial class V_MemberNew_Window : Window
    {
        public V_MemberNew_Window()
        {
            InitializeComponent();
            comboBox_Specilised.ItemsSource = new cecbClasses.Data_Adapters.Member.Func_Members().getSpecializatioInformations();

        }


        private void textBox_ContactNo3_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex regex = new Regex("[^0-9]+");
            e.Handled = regex.IsMatch(e.Text);


        }

        private void textBox_ContactNo3_TextChanged(object sender, TextChangedEventArgs e)
        {

        }


        private void Email_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {

        }



        private void button_Save_Cancel_Click(object sender, RoutedEventArgs e)
        {

        }


        private bool validations()
        {
            bool validated = false;


            if (textBox_FirstName.Text.Length == 0)
            {
                MessageBox.Show("Enter First Name", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            else if (textBox_Address.Text.Length == 0)
            {
                MessageBox.Show("Enter Address", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (comboBox_Designation.SelectedIndex == -1)
            {
                MessageBox.Show("Select Designation", "Message", MessageBoxButton.OK, MessageBoxImage.Error);

            }
            else if (comboBox_Specilised.SelectedIndex == -1)
            {
                MessageBox.Show("Select Specilization", "Message", MessageBoxButton.OK, MessageBoxImage.Error);

            }
            else if (textBox_ContactNo1.Text.Length == 0 && textBox_ContactNo2.Text.Length == 0 && textBox_ContactNo3.Text.Length == 0)
            {

                MessageBox.Show("Enter Atleast One Contact No", "Message", MessageBoxButton.OK, MessageBoxImage.Error);


            }
            else if (textBox_ContactNo1.Text.Length != 0 && textBox_ContactNo1.Text.Length != 10)
            {

                MessageBox.Show("Enter valid Contact No", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

            }
            else if (textBox_ContactNo2.Text.Length != 0 && textBox_ContactNo2.Text.Length != 10)
            {

                MessageBox.Show("Enter valid Contact No", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

            }
            else if (textBox_ContactNo3.Text.Length != 0 && textBox_ContactNo3.Text.Length != 10)
            {

                MessageBox.Show("Enter valid Contact No", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

            }

            else if (textBox_Email1.Text.Length == 0 && textBox_Email2.Text.Length == 0)
            {
                MessageBox.Show("Enter Atleast One Email Address", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (textBox_Email2.Text.Length != 0 && (!Regex.IsMatch(textBox_Email2.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$")))
            {
                MessageBox.Show("Enter valid Email Address", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (textBox_Email1.Text.Length != 0 && (!Regex.IsMatch(textBox_Email1.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$")))
            {
                MessageBox.Show("Enter valid Email Address", "Message", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                validated = true;
            }
            return validated;
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {

            if (validations())
            {
                List<String> contactNo = new List<string>();
                if (textBox_ContactNo1.Text.Trim() != "")
                    contactNo.Add(textBox_ContactNo1.Text.Trim());
                if (textBox_ContactNo2.Text.Trim() != "")
                    contactNo.Add(textBox_ContactNo2.Text.Trim());
                if (textBox_ContactNo3.Text.Trim() != "")
                    contactNo.Add(textBox_ContactNo3.Text.Trim());

                List<String> employEmail = new List<string>();
                if (textBox_Email1.Text.ToString() != "")
                    employEmail.Add(textBox_Email1.Text.ToString());
                if (textBox_Email2.Text.ToString() != "")
                    employEmail.Add(textBox_Email2.Text.ToString());



               cecbClasses.Data_Adapters.Member.Func_Members memins = new cecbClasses.Data_Adapters.Member.Func_Members();
                if (comboBox_Designation.Text == "Consultent")
                {
                    if (memins.InsertConsultantBasicInfo(textBox_FirstName.Text.Trim(), textBox_Address.Text.Trim(), comboBox_Designation.Text, comboBox_Specilised.Text.Trim(), contactNo, employEmail, comboBox_Specilised.Text.Trim()))
                    {
                        MessageBox.Show("Informations Added Successfully!", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                        comboBox_Specilised.ItemsSource = new cecbClasses.Data_Adapters.Member.Func_Members().getSpecializatioInformations();
                        clear(); 

                    }
                }
                else if (comboBox_Designation.Text == "Team Leader")
                {
                    if (memins.insertTeamLeadeaderInformations(textBox_FirstName.Text.Trim(), textBox_Address.Text.Trim(), comboBox_Designation.Text, comboBox_Specilised.Text.Trim(), contactNo, employEmail,   comboBox_Specilised.Text.Trim()))
                    {
                        MessageBox.Show("Informations Added Successfully!", "Message", MessageBoxButton.OK, MessageBoxImage.Information);
                        comboBox_Specilised.ItemsSource = new cecbClasses.Data_Adapters.Member.Func_Members().getSpecializatioInformations();
                        clear();

                    }
                }
            }


        }

        public void clear()
        {
            textBox_FirstName.Clear();
            textBox_ContactNo1.Clear();
            textBox_ContactNo2.Clear();
            textBox_ContactNo3.Clear();
            textBox_Email1.Clear();
            textBox_Email2.Clear();
            comboBox_Designation.SelectedIndex = -1;
            comboBox_Specilised.SelectedIndex = -1;
            textBox_ContactNo1.Clear();
            textBox_Address.Clear();
             
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void NewButton_Click(object sender, RoutedEventArgs e)
        {
            clear();
            SaveButton.IsEnabled = true;

        }

        private void textBox_Designation_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboBox_Designation.SelectedIndex == 1)
                wrapPanelUserLogin.Visibility = System.Windows.Visibility.Collapsed;
            else
                wrapPanelUserLogin.Visibility = System.Windows.Visibility.Visible;
        }






        String _oldContactNo = "";
        String _memID = "";
        String _oldEmail = "";
        String _memIDEmail = "";









        private void Label_SourceUpdated(object sender, DataTransferEventArgs e)
        {

        }

        private void textBox_Modifyusername_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void textBox_Modifyusername_SelectionChanged(object sender, RoutedEventArgs e)
        {

        }

        private void textBox_Modifyusername_TextInput(object sender, TextCompositionEventArgs e)
        {

        }

        private void textBox_Modifyusername_IsHitTestVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {

        }

        private void comboBox_Specilised_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void textBoxUpdateContact_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void button_addContact1_Click(object sender, RoutedEventArgs e)
        {
            if (textBox_ContactNo2.IsVisible)
            {
                textBox_ContactNo3.Visibility = System.Windows.Visibility.Visible;
               // button_addContact3.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                textBox_ContactNo2.Visibility = System.Windows.Visibility.Visible;
            }
        }

        

        private void button_addContact3_Click(object sender, RoutedEventArgs e)
        {
            textBox_Email2.Visibility = System.Windows.Visibility.Visible;
        }


    }
}
